<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\service\weChat;

use EasyWeChat\Factory;
use think\facade\Config;

class WeChatService{
    /*
    //公众号配置
    private $app_id = 'wx7a697517ef14bb1a';                     //公众号appid
    private $secret_key = 'a415c47b1977e47f61ecc9215e3745e1';   //公众号秘钥
    //商户配置
    private $mch_id = '1526401891';                             //商户id
    private $mch_key = '717F00203CC111D45EAC5C3CA6612E26';      //小程序对应的微信商户秘钥
    private $cert_path = 'static\cert\apiclient_cert.pem';     //支付证书
    private $key_path = 'static\cert\apiclient_key.pem';       //支付秘钥证书
    */
    
     //公众号配置
    private $app_id = 'wxaf4980b928379553';                     //小程序appid
    private $secret_key = '7effa86b83a35d730587ca6e77b1fc6d';   //小程序秘钥
    //商户配置
    private $mch_id = '1648788556';                             //商户id
    private $mch_key = '8f608270f7f0461ee15f5fcabf8e1899';      //小程序对应的微信商户秘钥
    private $cert_path = 'static\cert\jingyu\apiclient_cert.pem';     //支付证书
    private $key_path = 'static\cert\jingyu\apiclient_key.pem';       //支付秘钥证书
    

    //初始化微信公众号类
    function init(){
        $config = [
            'app_id' => $this->app_id,
            'secret' => $this->secret_key,
            'token' => 'yingyangchian',
            'aes_key' => 'BvcT2CHcpeW8T5vffkc0dAh4z2TGyStVXeCrAZP7ZI0',
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            'oauth' => [                  //微信授权和回调参数设置
                'scopes'   => ['snsapi_userinfo'],
                'callback' => '/index/auth/wechat/back',
            ],
        ];
        //dd($config);
        $weChatService = Factory::officialAccount($config);
        return $weChatService;
    }

    //获取基本的配置
    function getConfig(){
        return [
            'app_id' => $this->app_id,
            'secret' => $this->secret_key,
            'mch_id' => $this->mch_id,
        ];
    }

    /**
     * 公众号支付
     * @return \EasyWeChat\Payment\Application
     */
    function weChatPay($data=[]){
        $config = [
            'app_id' => $this->app_id,
            'key' => $this->mch_key,
            'mch_id' => $this->mch_id,
            'cert_path' => app()->getRootPath().$this->cert_path,
            'key_path' => app()->getRootPath().$this->key_path,
        ];
        if(isset($data['notify_url']) && $data['notify_url']) $config['notify_url'] = $data['notify_url'];
        $weChatService = Factory::payment($config);
        return $weChatService;
    }

}
